// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TurretScanner.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Navigation.Runtime.Motion
{
    /// <summary>
    /// Turret scanning controller.
    /// </summary>
    public class TurretScanner
    {
        /// <summary>
        /// Target headings through which to cycle.
        /// </summary>
        private readonly double[] targetHeadings;

        /// <summary>
        /// Index into target headings.
        /// </summary>
        private int headingIndex = 0;

        /// <summary>
        /// Direction cycling through target headings.
        /// </summary>
        private int direction = 1;

        /// <summary>
        /// Initializes a new instance of the TurretScanner class.
        /// </summary>
        /// <param name="targetHeadings">Target headings through which to cycle.</param>
        public TurretScanner(double[] targetHeadings)
        {
            this.targetHeadings = targetHeadings;
        }

        /// <summary>
        /// Calculate (potentially new) target heading given current turret state.
        /// </summary>
        /// <remarks>
        /// Continue to current target until stopped (velocity zero), then
        /// choose the next target; scanning left, right, left, right, ...
        /// </remarks>
        /// <param name="position">Current position (in radians).</param>
        /// <param name="velocity">Current turret velocity.</param>
        /// <returns>Target to be set if appropriate.</returns>
        public double? GetUpdatedTargetHeading(double position, double velocity)
        {
            bool stopped = velocity == 0;
            if (stopped)
            {
                this.headingIndex += this.direction;
                if (this.headingIndex == 0 || this.headingIndex == this.targetHeadings.Length - 1)
                {
                    this.direction *= -1;
                }

                return this.targetHeadings[this.headingIndex];
            }

            return null;
        }
    }
}
